package com.batchplatform.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.batchplatform.system.service.SysManageService;
import com.batchplatform.common.entity.system.System;

@Controller
public class SysManageController {
	@Autowired
	private SysManageService service;
	
	@RequestMapping("/displaySystems.do")
	public String displaySystems(Model model) {
		model.addAttribute("systemList", service.getSystems());
		return "system/manage/systems";
	}
	
	
	@RequestMapping("/toAddSystem")
    public String toAdd(){
        return "system/manage/addSystem";
    }
	
//	@RequestMapping("/saveSystem.do")
//	public String saveSystem(@RequestParam(value ="name")String name,
//						   @RequestParam(value ="desc",required = false)String desc,
//						   Model model) {
//		service.addSystem(name, desc);		
//		return displaySystems(model);
//	}
	
	@RequestMapping("/saveSystem.do")
	public String saveSystem(System sys, Model model) {
		service.addSystem(sys);		
		return displaySystems(model);
	}
	
	@RequestMapping("/toUpdateSystem")
    public String toUpdate(int id, Model model){
		System system = service.getSystem(id);
        model.addAttribute("system",system);
        return "system/manage/updateSystem";
    }
	
//	@RequestMapping("/updateSystem.do")
//	public String updateConfig(@RequestParam("id") int id,
//			@RequestParam("updateValue")String value,
//			@RequestParam("type")String type,
//			Model model){
//		service.updateSystem(id, value, type);
//		return displaySystems(model);
//	}
	
	@RequestMapping("/updateSystem.do")
	public String updateConfig(System sys,
			Model model){
		service.updateSystem(sys);
		return displaySystems(model);
//		return "redirect:/list";
	}
    
	
//	@RequestMapping("/deleteSystem.do")
//	public String deleteSystem(@RequestParam("id") int[] ids, Model model) {
//		service.deleteSystems(ids);
//		return displaySystems(model);
//	}
	@RequestMapping("/deleteSystem")
	public String deleteSystem(@RequestParam("id") int id, Model model) {
		service.deleteSystem(id);
		return displaySystems(model);
	}
}
